﻿using BaseDll;
using MotionIoLib;
using Newtonsoft.Json;
using OtherDevice;
using System;
using System.ComponentModel;
using System.Threading;

namespace ModuleUnit
{
    public class Config_TrayOut
    {
        [Category("工站参数"), DisplayName("1.出料请求 "), Description("出料请求 ")]
        public string str_出料请求 { set; get; } = "";
        [Category("工站参数"), DisplayName("2.出料完成"), Description("出料完成")]
        public string str_出料完成 { set; get; } = "";
        [Category("工站参数"), DisplayName("3.出料许可"), Description("出料许可")]
        public string str_出料许可 { set; get; } = "";
        [Category("工站参数"), DisplayName("4.出OK料"), Description("出OK料")]
        public string str_出OK料 { set; get; } = "";
        [Category("工站参数"), DisplayName("5.出NG料"), Description("出NG料")]
        public string str_出NG料 { set; get; } = "";
        [Category("工站参数"), DisplayName("5.后段完成"), Description("后段完成")]
        public bool b_后段完成 { set; get; } = true;
        [Category("工站参数"), DisplayName("6.流水线式"), Description("流水线式")]
        public bool b_流水线式 { set; get; } = true;
        [Category("工站参数"), DisplayName("7.滤波时长"), Description("滤波时长")]
        public int n_滤波时长 { set; get; } = 20;

    }
    [Description("出料模块")]
    public class Module_TrayOut : ModuleBase
    {
        public Config_TrayOut config = null;
        public Module_TrayOut(object config, string strName) : base( strName)
        {
            if (config == null)
            {
                this.config = new Config_TrayOut();
            }
            else
            {
                this.config = JsonConvert.DeserializeObject<Config_TrayOut>(config.ToString());

            }
            this.obj_Config = this.config;
        }
        public bool Function_等待出料开始(bool b_Ok, bool bmanual,Func<bool> Function_IsConnect=null)
        {
            if (config.b_后段完成)
            {
                config.str_出料请求.SetIO(true);
                b_OutputIOState = true;
            }
                
            if (config.str_出OK料 != "")
                config.str_出OK料.SetIO(b_Ok);
            while (true)
            {
                if (bmanual)
                {
                    if (GlobalVariable.g_StationState != StationState.StationStateStop)
                        throw new Exception("手动停止");
                }
                else
                {
                    if (GlobalVariable.g_StationState != StationState.StationStateRun && GlobalVariable.g_StationState != StationState.StationStatePause)
                        throw new Exception("自动停止");
                }

                if (BaseDll.GlobalVariable.g_StationState == BaseDll.StationState.StationStatePause && !config.b_流水线式)
                    continue;
                if (IOMgr.GetInstace().Function_ReCheckIO(config.str_出料许可, true, config.n_滤波时长))
                    break;
                if(Function_IsConnect!=null)
                {
                    if(!Function_IsConnect())
                    {
                        if (config.b_后段完成)
                        {
                            config.str_出料请求.SetIO(false);
                            b_OutputIOState = false;
                        }
                            
                        return false;
                    }
                }
                Thread.Sleep(5);
            }
            if (config.b_后段完成)
            {
                config.str_出料请求.SetIO(false);
                b_OutputIOState = false;
            }
               
            else
            {
                config.str_出料完成.SetIO(false);
                config.str_出料请求.SetIO(true);
                b_OutputIOState = true;

            }
            return true;

        }
        public void Function_出料完成(bool bmanual)
        {
            if (config.b_后段完成)
            {
                while (true)
                {
                    if (bmanual)
                    {
                        if (GlobalVariable.g_StationState != StationState.StationStateStop)
                            throw new Exception("手动停止");
                    }
                    else
                    {
                        if (GlobalVariable.g_StationState != StationState.StationStateRun && GlobalVariable.g_StationState != StationState.StationStatePause)
                            throw new Exception("自动停止");
                    }

                    if (BaseDll.GlobalVariable.g_StationState == BaseDll.StationState.StationStatePause && !config.b_流水线式)
                        continue;
                    if (IOMgr.GetInstace().Function_ReCheckIO(config.str_出料完成, true, config.n_滤波时长))
                        break;
                    Thread.Sleep(5);
                }
            }
            else
            {
                config.str_出料请求.SetIO(false);
                config.str_出料完成.SetIO(true);
                b_OutputIOState = false;
            }
        }

        bool b_OutputIOState = false;
        public void PauseDeal()
        {
            b_OutputIOState = config.str_出料请求.GetOutput();
            config.str_出料请求.SetIO(false);

        }
        public void ResumeDeal()
        {
            config.str_出料请求.SetIO(b_OutputIOState);

        }
        public void StopDeal()
        {
            if(config.str_出料请求!="")
               config.str_出料请求.SetIO(false);
            if (config.str_出料完成 != ""&& !config.b_后段完成)
                config.str_出料完成.SetIO(false);
        }
    }
}